package com.chensf.studydemo.guide.animation

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.annotation.DrawableRes
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.chensf.studydemo.R
import kotlinx.android.synthetic.main.fragment_grid.*

class GridFragment : Fragment() {
    companion object {
        fun newInstance(): GridFragment {
            return GridFragment()
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_grid, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val imageArray = arrayOf(
            R.drawable.animal_2024172,
            R.drawable.beetle_562035,
            R.drawable.bug_189903,
            R.drawable.butterfly_417971,
            R.drawable.butterfly_dolls_363342,
            R.drawable.dragonfly_122787,
            R.drawable.dragonfly_274059,
            R.drawable.dragonfly_689626,
            R.drawable.hover_fly_61682,
            R.drawable.hoverfly_546692
        )
        recyclerView.layoutManager = GridLayoutManager(requireContext(),2)
        recyclerView.adapter = object : RecyclerView.Adapter<ImageViewHolder>() {
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ImageViewHolder =
                ImageViewHolder(layoutInflater.inflate(R.layout.view_grid_item, parent, false))

            override fun getItemCount(): Int = imageArray.size

            override fun onBindViewHolder(holder: ImageViewHolder, position: Int) {
                holder.imageView.setImageResource(imageArray[position])
            }
        }
    }

}

class ImageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    var imageView: ImageView = itemView.findViewById<ImageView>(R.id.imageView)
}